﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CayleyTree
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (graphics == null) graphics = this.CreateGraphics();
            else
                graphics.Clear(Color.White);
            drawCayleyTree(N, 200, 310, Leng, -Math.PI / 2);
        }

        private Graphics graphics;
        public static Pen pen = Pens.Blue;
        public static double th1 = 30 * Math.PI / 180;
        public static double th2 = 20 * Math.PI / 180;
        public static double per1 = 0.6;
        public static double per2 = 0.7;
        public static int N = 10;
        //public static double X0 = 200;
        //public static double Y0 = 310;
        public static double Leng = 100;
        public static double Th = -Math.PI / 2;

        void drawCayleyTree(int n,
                double x0, double y0, double leng, double th)
        {
            if (n == 0) return;

            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1);

            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }
        void drawLine(double x0, double y0, double x1, double y1)
        {
            graphics.DrawLine(
                pen,
                (int)x0, (int)y0, (int)x1, (int)y1);
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();
        }
    }
}
